﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using EislaufScb.Model.Repository.Organisation;

namespace EislaufScb.WebForms.ViewModel.Person
{
    public class TrainerViewModel : PersonViewModel
    {
        public List<EislaufScb.Model.CurriculumViateEntry> CurriculumViateEntries { get; set; }

        public List<EislaufScb.Model.AdditionalTrainerData> TrainerData { get; set; }

        public List<EislaufScb.Model.Profession> Professions { get; set; }

        public TrainerViewModel()
        {
            CurriculumViateEntries = new List<Model.CurriculumViateEntry>();
            TrainerData = new List<Model.AdditionalTrainerData>();
            Professions = new List<Model.Profession>();
        }
        public static List<TrainerViewModel> GetTrainers()
        {
            PersonRepository repository = new PersonRepository(new Model.ClubModelContainer());
            List<TrainerViewModel> result = new List<TrainerViewModel>();

            List<EislaufScb.Model.Person> trainers = null;

            using (repository)
            {
                trainers = repository.GetPersonByType("Trainer");

                foreach (var person in trainers)
                {
                    TrainerViewModel trainer = new TrainerViewModel();
                    trainer.FirstName = person.FirstName;
                    trainer.MiddleName = person.MiddleName;
                    trainer.LastName = person.LastName;
                    trainer.Email = person.EMail;
                    trainer.AreaOfResponsibility = person.AreaOfResponsibility;

                    trainer.SubscriberNumbers = new List<string>();
                    foreach (var phone in person.Phone)
                    {
                        trainer.SubscriberNumbers.Add(phone.SubScriberNumber);
                    }

                    trainer.StreetName = person.Address.FirstOrDefault().Street.Name;
                    trainer.StreetNumber = person.Address.FirstOrDefault().Street.Number;
                    trainer.StreetExtension = person.Address.FirstOrDefault().Street.Extension;
                    trainer.CityPostalCode = person.Address.FirstOrDefault().City.Postalcode;
                    trainer.CityName = person.Address.FirstOrDefault().City.Name;

                    string address = string.Format("{0} {1} {2} <BR/> {3} {4}", trainer.StreetName, trainer.StreetNumber, trainer.StreetExtension, trainer.CityPostalCode, trainer.CityName);
                    trainer.Address = address;

                    foreach (var cv in person.CurriculumViateEntry)
                    {
                        trainer.CurriculumViateEntries.Add(cv);
                    }
                    foreach (var profession in person.Profession)
                    {
                        trainer.Professions.Add(profession);
                    }

                    foreach (var trainerdata in person.AdditionalTrainerData)
                    {
                        trainer.TrainerData.Add(trainerdata);
                    }

                    result.Add(trainer);
                }

                return result;
            }
        }
    }
}